import * as api from '../api/api';
import request from '../utils/request';
import queryString from 'query-string';

export default {
    namespace: 'role',
    state: {
        role: {},
        roles: { items: [] },
        roleSearchs:{keywords:""}
    },
    effects: {
        /**
         * 获取角色列表
         * @param param0 
         * @param param1 
         */
        *getRoles({ payload }, { call, put }) {
            const { keywords } = payload;
            const formKeywords = keywords;
            const roles: api.AbpApplicationServicesDtoListResultDtoSingleUserMallRolesVORoleVO =
                yield request(api.ManageroleManageApiFetchParamCreactor.manageroleManageGetRolesAsync({formKeywords}))
            yield put({ type: 'save', payload: { roles, roleSearchs:{ keywords} } });
        },
        /**
         * 获取角色详情
         * @param param0 
         * @param param1 
         */
        *getRole({ payload }, { call, put }) {
            const formId = payload.id;
            const role: api.SingleUserMallRolesVORoleVO =
                yield request(api.ManageroleManageApiFetchParamCreactor.manageroleManageGetRoleAsync({
                    formId,
                }))
            yield put({ type: 'save', payload: { role } });
        },
        /**
         * 保存角色
         * @param param0 
         * @param param1 
         */
        *saveRole({ payload }, { call, put }) {
            const { form, callback } = payload;
            yield request(api.ManageroleManageApiFetchParamCreactor.manageroleManageSaveRoleAsync(
                { form }
            ));
            callback && callback(true);
        },
        /**
         * 删除角色
         * @param param0 
         * @param param1 
         */
        *remove({ payload }, { call, put, select }) {
            const { form, callback } = payload;
            yield request(api.ManageroleManageApiFetchParamCreactor.manageroleManageRemoveRoleAsync(
                { form }
            ));
            const { roleSearchs } = yield select(({ role }) => role);
            yield put({ type: 'getRoles', payload: { ...roleSearchs }});
            callback && callback(true);
        },
    },
    reducers: {
        save(state, action) {
            return { ...state, ...action.payload };
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname, search }) => {
                switch (pathname) {
                    case '/role/roles':
                        dispatch({ type: 'getRoles', payload: { keywords:"" } });
                        break;
                    case '/role/role':
                        const { id } = queryString.parse(search);
                        if (id) {
                            dispatch({ type: 'getRole', payload: { id } });
                        }
                        else {
                            dispatch({ type: 'save', payload: { role: {} } });
                        }
                        break;
                }
            });
        },
    },
};